package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.ApplicationVariable;
import com.example.demo.common.PasswordTools;
import com.example.demo.common.UserSessionTools;
import com.example.demo.config.Captcha;
import com.example.demo.entity.UserInfo;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/reg")
    public AjaxResult add(UserInfo userInfo,HttpServletRequest request){
        //1.非空判断
//        if (userInfo == null || userInfo.getUsername() == null || userInfo.getUsername().equals(""))
//            return AjaxResult.fail(-1,"参数有误");
        if (userInfo == null || !StringUtils.hasLength(userInfo.getUsername())
             || !StringUtils.hasLength(userInfo.getPassword()))
            return AjaxResult.fail(-1,"参数有误");

        //2.调用Service 执行添加方法
         userInfo.setPassword(PasswordTools.encrypt(userInfo.getPassword()));//加盐加密
       int result =  userService.reg(userInfo);
        return AjaxResult.success(result);
    }
    @RequestMapping("/login")
    public AjaxResult login(String username, String password, HttpServletRequest request){
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password))
            return AjaxResult.fail(-1,"参数有误");
        UserInfo userInfo = userService.login(username);
        if (userInfo == null || userInfo.getId() <= 0){
            return AjaxResult.fail(-2,"用户名或密码错误");
        }

       // if (!password.equals(userInfo.getPassword())){
        //解密 验证
        if ( !PasswordTools.decrypt(password,userInfo.getPassword())){
            return AjaxResult.fail(-2,"用户名或密码错误");
        }
        //把当前用户存储到session中
        HttpSession session = request.getSession(true);
        session.setAttribute(ApplicationVariable.SESSION_USERINFO_KEY,userInfo);
        return AjaxResult.success(1);
    }
    @RequestMapping("/logout")
    public AjaxResult logout(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        session.removeAttribute(ApplicationVariable.SESSION_USERINFO_KEY);
        return AjaxResult.success(1);
    }
    /*
       判断当前用户是否登录
    */
    @RequestMapping("/islogin")
    public AjaxResult isLogin(HttpServletRequest request){
        if (UserSessionTools.getLoginUser(request) == null){
            //未登录
            return AjaxResult.success(-1);
        }else {
            return AjaxResult.success(1);
        }

    }
    @RequestMapping("/modify")
    public AjaxResult passwordModify(UserInfo userInfo,HttpServletRequest request){
        if (userInfo == null || !StringUtils.hasLength(userInfo.getUsername())
                || !StringUtils.hasLength(userInfo.getPassword()))
            return AjaxResult.fail(-1,"参数有误");
        UserInfo user = UserSessionTools.getLoginUser(request);
        //加盐加密
        userInfo.setPassword(PasswordTools.encrypt(userInfo.getPassword()));
        userInfo.setId(user.getId());
        int result = userService.passwordModify(userInfo);
        return AjaxResult.success(result);
    }
}
